package logic

import (
	"context"
	"xialou_platform/app/errorx"
	"xialou_platform/services/system/model"

	"xialou_platform/services/system/rpc/internal/svc"
	"xialou_platform/services/system/rpc/systemClient"

	"github.com/zeromicro/go-zero/core/logx"
)

type SysUserByIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSysUserByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SysUserByIdLogic {
	return &SysUserByIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SysUserByIdLogic) SysUserById(in *systemClient.IdBody) (*systemClient.SysUser, error) {
	sysUser, err := l.svcCtx.SysUserModel.FindOne(l.ctx, in.Id)
	if err != nil {
		if err == model.ErrNotFound {
			return nil, errorx.NewDefaultError("用户不存在。")
		}
		return nil, errorx.NewReasonError("网络出小差了，请重试刷新再试。", err.Error())
	}

	return &systemClient.SysUser{
		Id:         sysUser.Id,
		DeptId:     sysUser.DeptId,
		Avatar:     sysUser.Avatar,
		Name:       sysUser.Name,
		Password:   sysUser.Password,
		Salt:       sysUser.Salt,
		Gender:     sysUser.Gender,
		Email:      sysUser.Email,
		Phone:      sysUser.Phone,
		Status:     sysUser.Status,
		CreateTime: sysUser.CreateTime.Unix(),
		CreateBy:   sysUser.CreateBy,
		UpdateTime: sysUser.UpdateTime.Unix(),
		UpdateBy:   sysUser.UpdateBy,
		DelFlag:    sysUser.DelFlag,
		Role:       sysUser.Role,
	}, nil
}
